<template>
  <div class="my-container">
    <!-- 以登录 -->
    <div class="my-header yes-header" v-if="user">
      <div class="top">
        <div class="left">
          <!-- 头像 -->
          <van-image class="head" round :src="userInfo.photo" fit="cover" />
          <span class="text">{{userInfo.name}}</span>
        </div>
        <div class="right">
          <van-button type="default" size="mini" round to="/user/personal">编辑资料</van-button>
        </div>
      </div>
      <div class="bottom">
        <div class="data-item">
          <span>{{userInfo.art_count}}</span>
          <span>头条</span>
        </div>
        <div class="data-item">
          <span>{{userInfo.follow_count}}</span>
          <span>关注</span>
        </div>
        <div class="data-item">
          <span>{{userInfo.fans_count}}</span>
          <span>粉丝</span>
        </div>
        <div class="data-item">
          <span>{{userInfo.like_count}}</span>
          <span>获赞</span>
        </div>
      </div>
    </div>
    <!-- 未登录 -->
    <div class="my-header not-header" v-else>
      <div class="login-btn">
        <img src="~@/assets/mobile.png" alt />
        <span @click="$router.push('/login')">登录 / 注册</span>
      </div>
    </div>
    <!-- 导航 -->
    <van-grid :column-num="2" class="grid-nav" clickable>
      <van-grid-item text="收藏">
        <i slot="icon" class="iconfont iconshoucang"></i>
        <span slot="text">收藏</span>
      </van-grid-item>
      <van-grid-item text="历史">
        <i slot="icon" class="iconfont iconlishi"></i>
        <span slot="text">历史</span>
      </van-grid-item>
    </van-grid>
    <!-- 单元格 -->
    <van-cell title="消息通知" is-link />
    <van-cell title="小智同学" is-link />
    <van-cell title="退出登录" center class="quit" v-if="user" clickable @click="quitLogin" />
  </div>
</template>

<script>
import { mapState } from 'vuex'
import { UserInfo } from '@/api/user'
export default {
  name: 'myIndex',
  data() {
    return {
      userInfo: []
    }
  },
  created() {
    if (this.$store.state.user) {
      this.getUserInfo()
    }
  },
  mounted() {},
  computed: {
    ...mapState(['user'])
  },
  methods: {
    // 退出登录
    quitLogin() {
      this.$dialog.confirm({
        title: '确认退出登录？'
      })
        .then(() => {
          this.$store.commit('getUserList', null)
        })
        .catch(() => {
          console.log('退出登录失败')
        })
    },
    // 获取用户信息
    async getUserInfo() {
      try {
        const { data } = await UserInfo()
        this.userInfo = data.data
      } catch (err) {
        console.log('获取用户信息失败')
      }
    }
  }
}
</script>

<style scoped lang="less">
.my-header {
  height: 361px;
  background: url(~@/assets/banner.png);
  background-size: cover;
}
.not-header {
  display: flex;
  justify-content: center;
  align-items: center;
  .login-btn {
    display: flex;
    justify-content: center;
    align-items: center;
    flex-direction: column;
    img {
      width: 132px;
      height: 132px;
      margin-bottom: 15px;
    }
    span {
      font-size: 28px;
      color: #fff;
    }
  }
}
.yes-header {
  color: #fff;
  .top {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 76px 32px 23px;
    .left {
      display: flex;
      justify-content: center;
      align-items: center;
      .head {
        width: 132px;
        height: 132px;
        margin-right: 25px;
        border: 3px solid #fff;
      }
      .text {
        font-size: 30px;
      }
    }
    .right {
      color: #666;
    }
  }
  .bottom {
    display: flex;
    height: 130px;
    .data-item {
      flex: 1;
      display: flex;
      justify-content: center;
      align-items: center;
      flex-direction: column;
      :nth-child(1) {
        font-size: 36px;
      }
      :nth-child(2) {
        font-size: 23px;
      }
    }
  }
}
.grid-nav {
  margin-bottom: 10px;
  .iconfont {
    font-size: 45px;
  }
  .iconshoucang {
    color: #eb5253;
  }
  .iconlishi {
    color: #ff9d1d;
  }
  span {
    margin-top: 8px;
    font-size: 28px;
    color: #000;
  }
}
.quit {
  text-align: center;
  color: #d86262;
  font-size: 30px;
  margin-top: 10px;
}
</style>
